<?php
include_once("session.php");
include_once('db.php');
include_once('constants.php');

if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
  $username = $_POST['actusername'];
  $tid = $_POST['acttid'];   
}
else
{
  $username = $_GET['actusername'];
  $tid = $_GET['acttid'];
  $email = $_GET['actemail'];   
}

if($username)
{	
	// now process form      
  $error = false;
  $errordesc = "";
  // check data
  if ( empty($username))
  {
    $error = true;
    $errordesc = "Invalid user name.";
  }
  if ( empty($tid))
  {
    $error = true;
    $errordesc = "Invalid activation id.";
  }
      
  if ( !$error)
  {
    $conn = &ADONewConnection($dbType);
    $conn->PConnect($dbServer, $dbUser, $dbPassword, $dbName);
    //$conn->debug = true;      
    $query = "select id_user, login, trackid, state from user where lower(login)=".$conn->qstr(strtolower($username))." and trackid=".$conn->qstr($tid);
    $rs = &$conn->Execute($query);    
    if ( !$rs->EOF)
    {
      // activate user      
      $query = "update user set state = 1 where login=".$conn->qstr(strtolower($username));  
      $conn->Execute($query);
                     
      // now go somewhere else
	    header("Location: download.php?type=1&userLogin=$username&userTid=$tid");
      exit;		    
    }
    else
    {
      $error = true;
      $errordesc = "Invalid User Name or Activation Id. Please try again.";
    }
  }		 
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title><?php echo $product_name;?></title>
    <link rel="stylesheet" href="css/main.css" type="text/css">
    
    <script type="text/javascript" src="scripts/md5.js"></script>
    <script type="text/javascript">
      //<![CDATA[
      function activateSubmit() {
        if (document.forms["activateform"].actusername.value == '') {
          var error = document.getElementById("error");
          error.innerHTML = "Please enter your username.";
          document.forms["activateform"].actusername.focus();
          return false;
        }
        if (document.forms["activateform"].acttrackid.value == '') {
          var error = document.getElementById("error");
          error.innerHTML = "Please enter your Track Id.";
          document.forms["activateform"].acttrackid.focus();
          return false;
        }
       
        return true;
      }
      //]]>
    </script>
    
  </head>
  <body>    
    <div id="main">          
    <?php 
      include ("header.php");      
    ?>       
    <div id="content">             
      <div id="content">               
      <h1>Step 2. Activate your account.</h1>      
      <br>
      <p>
      An activation message was sent to your email address
      <?php
        if ($email)
        { 
          echo '"'.$email.'"';
        }
      ?>. 
      Please enter your username and activation ID bellow or click on the link provided in that message.      
      </p>      
      <br>
      <p class="error" id="error">
      <?php         
        if ($error)
        {
          echo $errordesc;
        }
      ?>
      </p>
      <div id="mainform">      
        <form id="activateform" class="mform" action="activate.php" onsubmit="return activateSubmit();" method="post" enctype="multipart/form-data">
        <table>
          <tr>
            <td><label for="actusername" accesskey="u">User Name:</label></td>
            <td class="btext"><input type="text" name="actusername" id="actusername" size="25" value="<?php echo $username; ?>"/></td>
          </tr>
          <tr>
            <td><label for="acttid" accesskey="p">Activation Id:</label></td>
            <td class="btext"><input type="text" name="acttid" id="acttid" size="25" value="<?php echo $tid; ?>"/></td>
          </tr>          
          <tr><td colspan="2"><hr/></td></tr>
          <tr><td></td><td class="btn" colspan="2"><input type="submit" id="activate" name="signup" value="Activate"/></td></tr>
        </table>
        </form>
      </div>      
    <br/>    
    <?php include ("footer.php"); ?>
    </div><!-- main -->
    <?php         
        include ("ga.php");
    ?>
  </body>
</html>
